/**
  ******************************************************************************
  * @文件名           : dri_slaver.c
  * @作者             : citunhk
  * @功能介绍         : 从机驱动
  *	@日期             : 2022.11.1
  ******************************************************************************
  * @注意
  *
  * 本文件中不应调用上层文件
  *
  ******************************************************************************
  *更改记录
  *日期 修改人 修改内容
  *
  *
  *
  ******************************************************************************
  */
#include "dri_slaver.h"
#include "bsp_usart.h"
#include "fml_crc.h"
#include "sys_data.h"
#include "dri_flash_sst.h"

uint8_t pBus485_Slaver_TxBuffer[RS485_SELVER_TX_NUM];
uint8_t gBus485_Slaver_RxBuffer[RS485_SELVER_RX_NUM];
uint8_t gBus485_Slaver_RxCounter=0x00;   //从机接收计数
uint8_t pBus485_Slaver_TxCounter=0x00;   //从机发送计数
CommParam  Comm_Param;

uint8_t gErr_Code_SMC = 0;  //SMC通讯错误代码

static void DRI_ModbusRTU_ProcessSlaverRxCmd03(void);
static void DRI_ModbusRTU_ProcessSlaverRxCmd06(void);
static void DRI_ModbusRTU_ProcessSlaverRxCmd16(void);

static void DRI_MoubusRTU_updataRegData(void);
static void DRI_MoubusRTU_updataSysData(void);
static uint16_t DRI_MoubusRTU_getRegData(uint16_t add);
static void DRI_MoubusRTU_setRegData(uint16_t add);
static void DRI_MoubusRTU_setRegDatas(uint16_t add, uint8_t cnt);

/**
* @函数名	DRI_USART3_BaudRate
* @功能	    USART3串口波特率更改
* @入口参数	mode 串口波特率 0 9600
*                          1 19200
* @返回值	无
*/
void DRI_USART3_BaudRate(uint8_t mode)
{
	USART3_Config_BaudRate(mode);
}

/**
* @函数名	DRI_USART3_Config_Switch
* @功能	    USART3串口配置切换
* @入口参数	mode 串口模式 0 9600 校验无 数据位8 停止位1
*                         1 9600 校验偶校验 数据位7 停止位1
* @返回值	无
*/
void DRI_USART3_Config_Switch(uint8_t mode, uint32_t baudRate)
{
	USART3_Config_Switch(mode, baudRate);
}

/**
* @函数名	DRI_MakeModbusSlaverUpMsgCrc
* @功能	    生成RS485从机数据数组中的CRC
* @入口参数	无
* @返回值	无
*/
void DRI_MakeModbusSlaverUpMsgCrc(void)
{
	uint16_t uiCRCResult;

//	Crc16Arg.pCrc16ArgsPuchMsg = pBus485_Slaver_TxBuffer;
//	Crc16Arg.crc16ArgsPuchMsgLen = pBus485_Slaver_TxCounter;
	uiCRCResult = Crc16(pBus485_Slaver_TxBuffer, pBus485_Slaver_TxCounter);
//	uiCRCResult = Crc16Arg.returnValue;

	pBus485_Slaver_TxBuffer[pBus485_Slaver_TxCounter++] = uiCRCResult & 0x00FF;
	pBus485_Slaver_TxBuffer[pBus485_Slaver_TxCounter++] = uiCRCResult >> 8;
}

/**
* @函数名	DRI_SlaverUpStartTx
* @功能	    RS485从机发送数据
* @入口参数	无
* @返回值	无
*/
void DRI_SlaverUpStartTx(void)
{
	BSP_RS485_Slaver(RS485_Slaver_Tx);
	BSP_Usart_SendU8(USART3,pBus485_Slaver_TxBuffer,pBus485_Slaver_TxCounter);
	LL_mDelay(2);
	BSP_RS485_Slaver(RS485_Slaver_Rx);
}

/**
* @函数名	DRI_ModbusRTU_Slaver_RX_Process
* @功能	    RS485从机数据处理
* @入口参数	无
* @返回值	无
*/
void DRI_ModbusRTU_Slaver_RX_Process(void)
{
	uint16_t puiCRCResult;
	uint8_t pModbusUpFrameSlaverStatus = 1;
	if(gBus485_Slaver_RxCounter < 3) //如果数据字节少于3
	{
		pModbusUpFrameSlaverStatus = 1;
	}
	else
	{
		uint16_t crcdata;
//		Crc16Arg.pCrc16ArgsPuchMsg = gBus485_Slaver_RxBuffer;  //获取等待CRC数组地址
//		Crc16Arg.crc16ArgsPuchMsgLen = gBus485_Slaver_RxCounter - 2;  //获取等待CRC数组长度  不包含原CRC
		puiCRCResult = Crc16(gBus485_Slaver_RxBuffer, gBus485_Slaver_RxCounter - 2);
//		puiCRCResult = Crc16Arg.returnValue;  //得到CRC数值
		crcdata = (gBus485_Slaver_RxBuffer[gBus485_Slaver_RxCounter - 1] << 8)|(gBus485_Slaver_RxBuffer[gBus485_Slaver_RxCounter - 2]);
		gBus485_Slaver_RxCounter = 0;  //清除计数标志
		if((crcdata - puiCRCResult) == 0)//CRC校验通过
		{
			if((gBus485_Slaver_RxBuffer[0] == UP.Para.RS485_Addr) || (gBus485_Slaver_RxBuffer[0] == 0))
			{
				pModbusUpFrameSlaverStatus=0;  //接收成功
			}
		}
		else
		{
			pModbusUpFrameSlaverStatus=1;//接收失败
		}
	}
	if(pModbusUpFrameSlaverStatus==0)
	{
		if(gBus485_Slaver_RxBuffer[0])  //地址不为0
		{
			switch(gBus485_Slaver_RxBuffer[1])  //判断指令
			{
				case 3://读多个寄存器
					gSlaver_State = 0;
					DRI_ModbusRTU_ProcessSlaverRxCmd03();
					break;
				case 6://写单个寄存器
					gSlaver_State = 0;
					DRI_ModbusRTU_ProcessSlaverRxCmd06();
					break;
				case 16://写多个寄存器
					gSlaver_State = 0;
					DRI_ModbusRTU_ProcessSlaverRxCmd16();
					break;
				default:
				break;
			}
		}
	}
}

void DRI_ModbusRTU_ProcessSlaverRxCmd03(void)
{
	uint16_t uiStart;
	uint16_t uiQuantity;
	uint16_t uiEnd;
	uint16_t regData;
	uint16_t add;

	//字节个数
	uiQuantity = ((uint16_t)gBus485_Slaver_RxBuffer[4] << 8) + gBus485_Slaver_RxBuffer[5];
	if(uiQuantity > 0x7D)
	{
		//ExceptionResponse(0x03);
		return;
	}
	//起始地址
	uiStart = ((uint16_t)gBus485_Slaver_RxBuffer[2] << 8) + gBus485_Slaver_RxBuffer[3] + 1;
	//结束地址
	uiEnd = uiStart + uiQuantity;
//		if(ModbusUpListenOnly)
//		{
//			ModbusUpUartInit();
//			return;
//		}
	pBus485_Slaver_TxCounter = 0;
	pBus485_Slaver_TxBuffer[pBus485_Slaver_TxCounter++] = gBus485_Slaver_RxBuffer[0];
	pBus485_Slaver_TxBuffer[pBus485_Slaver_TxCounter++] = gBus485_Slaver_RxBuffer[1];
	pBus485_Slaver_TxBuffer[pBus485_Slaver_TxCounter++] = uiQuantity * 2;
	DRI_MoubusRTU_updataRegData();
	for(add = uiStart; add < uiEnd; add++)
	{
		regData = DRI_MoubusRTU_getRegData(add);//todo:根据不同的协议处理不同的数据
		pBus485_Slaver_TxBuffer[pBus485_Slaver_TxCounter++] = (uint8_t)(regData >> 8);
		pBus485_Slaver_TxBuffer[pBus485_Slaver_TxCounter++] = (uint8_t)(regData & 0x00ff);
	}
	DRI_MakeModbusSlaverUpMsgCrc();
	DRI_SlaverUpStartTx();
}

static void DRI_ModbusRTU_ProcessSlaverRxCmd06(void)
{
	uint16_t uiStart;

	uiStart = ((uint16_t)gBus485_Slaver_RxBuffer[2] << 8) + gBus485_Slaver_RxBuffer[3] + 1;
//	if(ModbusUpListenOnly)
//	{
//		ModbusUpUartInit();
//		return;
//	}
	pBus485_Slaver_TxCounter=0;
	pBus485_Slaver_TxBuffer[pBus485_Slaver_TxCounter++] = gBus485_Slaver_RxBuffer[0];
	pBus485_Slaver_TxBuffer[pBus485_Slaver_TxCounter++] = gBus485_Slaver_RxBuffer[1];

	pBus485_Slaver_TxBuffer[pBus485_Slaver_TxCounter++] = gBus485_Slaver_RxBuffer[2];
	pBus485_Slaver_TxBuffer[pBus485_Slaver_TxCounter++] = gBus485_Slaver_RxBuffer[3];

	pBus485_Slaver_TxBuffer[pBus485_Slaver_TxCounter++] = gBus485_Slaver_RxBuffer[4];
	pBus485_Slaver_TxBuffer[pBus485_Slaver_TxCounter++] = gBus485_Slaver_RxBuffer[5];

	DRI_MakeModbusSlaverUpMsgCrc();
	DRI_SlaverUpStartTx();

	DRI_MoubusRTU_setRegData(uiStart);
	DRI_MoubusRTU_updataSysData();

}

static void DRI_ModbusRTU_ProcessSlaverRxCmd16(void)
{
	uint16_t uiStart;
	uint16_t uiEnd;
	uint16_t uiQuantity;
	uint16_t add;
	uint8_t cnt;

	//寄存器个数
	uiQuantity = ((uint16_t)gBus485_Slaver_RxBuffer[4] << 8) + gBus485_Slaver_RxBuffer[5];
	if(uiQuantity > 0x7D)
	{
		//ExceptionResponse(0x03);
		return;
	}
	//寄存器起始地址
	uiStart = ((uint16_t)gBus485_Slaver_RxBuffer[2] << 8) + gBus485_Slaver_RxBuffer[3] + 1;
	//结束地址
	uiEnd = uiStart + uiQuantity;
	for(add = uiStart, cnt = 0; add < uiEnd; add++)
	{
		DRI_MoubusRTU_setRegDatas(add, cnt);
		cnt += 2;
	}
	pBus485_Slaver_TxCounter = 0;
	pBus485_Slaver_TxBuffer[pBus485_Slaver_TxCounter++] = gBus485_Slaver_RxBuffer[0];
	pBus485_Slaver_TxBuffer[pBus485_Slaver_TxCounter++] = gBus485_Slaver_RxBuffer[1];
	pBus485_Slaver_TxBuffer[pBus485_Slaver_TxCounter++] = gBus485_Slaver_RxBuffer[2];
	pBus485_Slaver_TxBuffer[pBus485_Slaver_TxCounter++] = gBus485_Slaver_RxBuffer[3];
	pBus485_Slaver_TxBuffer[pBus485_Slaver_TxCounter++] = gBus485_Slaver_RxBuffer[4];
	pBus485_Slaver_TxBuffer[pBus485_Slaver_TxCounter++] = gBus485_Slaver_RxBuffer[5];
	DRI_MakeModbusSlaverUpMsgCrc();
	DRI_SlaverUpStartTx();

	DRI_MoubusRTU_updataSysData();
}
static void DRI_MoubusRTU_updataRegData(void)
{
	//故障标志位
	gSlaver_FLT = gAlaFlag_FLT? 1: 0;

	if(FP.Para.Com_Mode == 0)//WIIP协议
	{
		//报警消音标志位
		if(gES_Buzzer == 0)  //消音
		{
		    gSlaver_ES_Buzzer = 1;  //消音
		}
		else
		{
		    gSlaver_ES_Buzzer = 0;  //未消音
		}

		//控制字1
		rSlaver_State_Control1 = (UP.Para.Type_Communication & 0x01) | ((gAlarmReset & 0x01)<<1) | ((gSlaver_ES_Buzzer & 0x01)<<2) | ((gState_Run & 0x01)<<8) | ((UP.Para.Type_FlowContr & 0x01)<<9) | ((gState_Return & 0x01)<<12);

		//报警标志位
		if((gAlaFlag_CompFLT == 1) || (gAlaFlag_HeaterFLT == 1) || ((gAlaFlag_LowTemp == 1) && (UP.Para.AlarmType_TempLL == 0)) || ((gAlaFlag_LowFlow == 1) && (UP.Para.AlarmType_FlowLL == 0)) || ((gAlaFlag_FlowSensor == 1) && (UP.Para.AlarmType_FlowSensor == 0)))
		{
		    gSlaver_Ala = 1;
		}
		else
		{
		    gSlaver_Ala = 0;
		}

		//维护报警
		if((gAlaFlag_CompMaint == 1) || (gAlaFlag_PumpMaint == 1) || (gAlaFlag_FilterMaint == 1))
		{
		    gSlaver_Maint_Alarm = 1;
		}
		else
		{
		    gSlaver_Maint_Alarm = 0;
		}

		//状态字1
		rSlaver_State_Chiller1 = (gSlaver_FLT & 0x01) | ((gSlaver_Ala & 0x01)<<1) | ((gSlaver_Maint_Alarm & 0x01)<<2) | ((gState_Run & 0x01)<<8) | ((gState_Return & 0x01)<<10);

		//传感器异常
		if((gAlaFlag_FlowSensor == 1) || (gAlaFlag_TempSensor1 == 1) || (gAlaFlag_TempSensor2 == 1) || (gAlaFlag_TempSensor3 == 1) || (gAlaFlag_PressInSensor == 1) || (gAlaFlag_PressOutSensor == 1) || (gAlaFlag_RefHighPressSensor == 1) || (gAlaFlag_RefLowPressSensor == 1) || (gAlaFlag_DISensor == 1) || (gAlaFlag_OutFlowSensor == 1) || (gAlmFlag_LevelSensor == 1))
		{
		    gSlaver_Sensor_Ala = 1;
		}
		else
		{
		    gSlaver_Sensor_Ala = 0;
		}

		//变频水泵故障
		if(rFlag_Comp != 0)  //存在故障
		{
		    gSlaver_Comp = 1;
		}
		else
		{
		    gSlaver_Comp = 0;
		}

		//报警字1
		rSlaver_State_Alarm1 = (gAlaFlag_LowLevelUP & 0x01) | ((gAlaFlag_LowLevel & 0x01)<<1) | ((gAlaFlag_OverFlow & 0x01)<<2) | ((gAlaFlag_COWLowFlow & 0x01)<<3) | ((gAlaFlag_RefPress1 & 0x01)<<4) | ((gAlaFlag_EStop & 0x01)<<8) | ((gSlaver_Sensor_Ala & 0x01)<<10) | ((gSlaver_Maint_Alarm & 0x01)<<11) | ((gSlaver_Communication_ERR & 0x01)<<13) | ((gAlaFlag_HighLevel & 0x01)<<14);

		//报警字2
		rSlaver_State_Alarm2 = (gAlaFlag_HighTemp & 0x01) | ((gAlaFlag_HighTemp1 & 0x01)<<1) | ((gAlaFlag_LowTemp1 & 0x01)<<2) | ((gAlaFlag_LowTemp & 0x01)<<3) | ((gAlaFlag_HL_OutPress & 0x01)<<4) | ((gAlaFlag_LL_OutPress & 0x01)<<5) | ((gAlaState_LowFlow & 0x01)<<6) | ((gFltState_LowFlow & 0x01)<<7) | ((gAlaFlag_LL_DI & 0x01)<<8) | ((gSlaver_Comp & 0x01)<<9) | ((gAlaFlag_PumpCom1 & 0x01)<<10) | ((gSlaver_AlaFlag_Press1 & 0x01)<<11) | ((gSlaver_AlaFlag_Press2 & 0x01)<<12);

	}
	else if(FP.Para.Com_Mode == 1)  //SMC_ASCII协议
	{
		//报警标志位
		if((gAlaFlag_CompFLT == 1) || (gAlaFlag_HeaterFLT == 1) ||
		    ((gAlaFlag_OverFlow == 1) && (UP.Para.AlarmType_OverFlow == 0)) ||
		    ((gAlaFlag_LowTemp == 1) && (UP.Para.AlarmType_TempLL == 0)) ||
		    ((gAlaFlag_LowFlow == 1) && (UP.Para.AlarmType_FlowLL == 0)) ||
		    ((gAlaFlag_FacLowFlow1 == 1) && (FP.Para.Type_LL_Flow == 0)) ||
		    ((gAlaFlag_FlowSensor == 1) && (UP.Para.AlarmType_FlowSensor == 0)) ||
		    ((gAlaFlag_HL_OutPress == 1) && (UP.Para.AlarmType_PressHL == 0)) ||
		    ((gAlaFlag_LL_OutPress == 1) && (UP.Para.AlarmType_PressLL == 0)) ||
		    ((gAlaFlag_PressOutSensor == 1) && (UP.Para.AlarmType_OutPressSensor == 0)) ||
		    (gAlaFlag_LowLevelUP == 1) || (gAlmFlag_LevelSensor == 1) ||
		    (gAlaFlag_HighLevel == 1) || (gAlaFlag_HL_OutPress == 1) ||
		    (gAlaFlag_DISensor == 1) || (gAlaFlag_LL_DI == 1) ||
		    (gAlaFlag_Communication == 1) || (gAlaFlag_CompMaint == 1) ||
		    (gAlaFlag_Comp2Maint == 1) || (gAlaFlag_OutFlowSensor == 1) ||
		    (gAlaFlag_PumpMaint == 1) || (gAlaFlag_FilterMaint == 1) ||
		    (gAlaFlag_TempSensor2 == 1) || (gAlaFlag_TempSensor3 == 1) ||
		    (gAlaFlag_RefPressSensor1 == 1) || (gAlaFlag_RefPress1 == 1) ||
		    (gAlaFlag_RefPressSensor2 == 1) || (gAlaFlag_RefPress2 == 1))
		{
		    gSlaver_Ala = 1;
		}
		else
		{
		    gSlaver_Ala = 0;
		}

		//状态字1
		rSlaver_State_Chiller1 = (gState_Run & 0x01) |
		                            ((gSlaver_FLT & 0x01)<<1) |
		                            ((gSlaver_Ala & 0x01)<<2) |
		                            ((UP.Para.Type_Communication & 0x01)<<5);

		//报警字1
		rSlaver_State_Alarm1 = (gAlaFlag_OverFlow & 0x01) |
		                        ((gAlaFlag_LowLevel & 0x01)<<4) |
		                        ((gAlaFlag_LowLevelUP & 0x01)<<5) |
		                        ((gAlaFlag_HighLevel & 0x01)<<6) |
		                        ((gAlaFlag_FacHighTemp & 0x01)<<8) |
		                        ((gAlaFlag_HighTemp & 0x01)<<10) |
		                        ((gAlaFlag_FacLowFlow1 & 0x01)<<11) |									 ((gAlaFlag_LowLevel & 0x01)<<4) |
		                        ((gAlaFlag_LowFlow & 0x01)<<12);
		//报警字2
		rSlaver_State_Alarm2 = ((gSlaver_Communication_ERR & 0x01)<<6) |
		                        ((gAlaFlag_PumpCom1 & 0x01)<<8);

		//控制字1
		rSlaver_State_Control1 = gState_Run & 0x0001;

	}
	else if(FP.Para.Com_Mode == 2)  //SMC_RTU协议
	{
		//报警标志位
		if((gAlaFlag_CompFLT == 1) || (gAlaFlag_HeaterFLT == 1) || ((gAlaFlag_LowTemp == 1) && (UP.Para.AlarmType_TempLL == 0)) || ((gAlaFlag_LowFlow == 1) && (UP.Para.AlarmType_FlowLL == 0)) || ((gAlaFlag_FlowSensor == 1) && (UP.Para.AlarmType_FlowSensor == 0)))
		{
		    gSlaver_Ala = 1;
		}
		else
		{
		    gSlaver_Ala = 0;
		}

		//维护报警
		if((gAlaFlag_CompMaint == 1) || (gAlaFlag_PumpMaint == 1) || (gAlaFlag_FilterMaint == 1))
		{
		    gSlaver_Maint_Alarm = 1;
		}
		else
		{
		    gSlaver_Maint_Alarm = 0;
		}

		//传感器异常
		if((gAlaFlag_FlowSensor == 1) || (gAlaFlag_TempSensor1 == 1) || (gAlaFlag_TempSensor2 == 1) || (gAlaFlag_PressInSensor == 1) || (gAlaFlag_PressOutSensor == 1))
		{
		    gSlaver_Sensor_Ala = 1;
		}
		else
		{
		    gSlaver_Sensor_Ala = 0;
		}

		//设备状态更新
		rSlaver_State_Chiller = (gState_Run & 0x01) | ((gSlaver_FLT & 0x01)<<1) | ((gSlaver_Ala & 0x01)<<2) | ((UP.Para.AlarmType_Maint & 0x02)<<2) | ((FP.Para.Type_Heater & 0x01)<<13) | ((UP.Para.Type_Start & 0x01)<<14);
		rSlaver_State_Alarm1 = ((gAlaFlag_LowLevel & 0x01)<<1) | ((gAlaFlag_FacHighTemp & 0x01)<<8) | ((gAlaFlag_HighTemp & 0x01)<<9) | ((gAlaFlag_LowTemp & 0x01)<<10);
		rSlaver_State_Alarm3 = ((gSlaver_Communication_ERR & 0x01)<<1) | ((gSlaver_Maint_Alarm & 0x01)<<3) | ((gSlaver_Sensor_Ala & 0x01)<<5) | ((gAlaFlag_COWLowFlow & 0x01)<<14);
		rSlaver_Operation = (gState_Run & 0x01) | ((UP.Para.Type_Communication & 0x01)<<1) | ((gAlarmReset & 0x01)<<2);

		//数据显示1
		switch(gSlaver_Data1)
		{
		case 0:
		    gSlaver_DisplayData1 = 0;  //空
		    break;
		case 1:
		    gSlaver_DisplayData1 = gPV_TempAbmi;  //环境温度
		    break;
		case 2:
		    gSlaver_DisplayData1 = gPV_Temp2;  //外部温度
		    break;
		default:
		    break;
		}
		//数据显示2
		switch(gSlaver_Data2)
		{
		case 0:
		    gSlaver_DisplayData2 = 0;  //空
		    break;
		case 1:
		    gSlaver_DisplayData2 = gPV_TempAbmi;  //环境温度
		    break;
		case 2:
		    gSlaver_DisplayData2 = gPV_Temp2;  //外部温度
		    break;
		default:
		    break;
		}
		//数据显示3
		switch(gSlaver_Data3)
		{
		case 0:
		    gSlaver_DisplayData3 = 0;  //空
		    break;
		case 1:
		    gSlaver_DisplayData3 = gPV_TempAbmi;  //环境温度
		    break;
		case 2:
		    gSlaver_DisplayData3 = gPV_Temp2;  //外部温度
		    break;
		default:
		    break;
		}
		//数据显示4
		switch(gSlaver_Data4)
		{
		case 0:
		    gSlaver_DisplayData4 = 0;  //空
		    break;
		case 1:
		    gSlaver_DisplayData4 = gPV_TempAbmi;  //环境温度
		    break;
		case 2:
		    gSlaver_DisplayData4 = gPV_Temp2;  //外部温度
		    break;
		default:
		    break;
		}

	}
}

static void DRI_MoubusRTU_updataSysData(void)
{
	static uint16_t pRS485_New_Run = 0;  //运行启动新模式
	static uint16_t pRS485_Old_Run = 0;  //运行启动上次模式
	static uint16_t pRS485_New_Mode = 0;  //通讯新模式
	static uint16_t pRS485_Old_Mode = 0;  //通讯上次模式
	static uint16_t pRS485_New_Alarm = 0;  //报警复位新状态
	static uint16_t pRS485_Old_Alarm = 0;  //报警复位上次状态
	static uint16_t pRS485_New_Buzzer = 0;  //报警消音新状态
	static uint16_t pRS485_Old_Buzzer = 0;  //报警消音上次状态

	DRI_Flash_Erase_4KByte(ADDR_USER_PARA);//写入前擦除
	DRI_Flash_AAI_WriteBuffer(&UP.flashBuff[0], ADDR_USER_PARA, 500);//写入用户初值到buff中

	if(FP.Para.Com_Mode == 0)//WIIP协议
	{
		pRS485_New_Run = ((rSlaver_State_Control1>>8) & 0x01);  //运行状态

		pRS485_New_Mode = (rSlaver_State_Control1 & 0x01);  //通讯模式
		if(pRS485_New_Mode != pRS485_Old_Mode)
		{
		    UP.Para.Type_Communication = pRS485_New_Mode;
		    pRS485_Old_Mode = pRS485_New_Mode;
		}

		pRS485_New_Alarm = ((rSlaver_State_Control1>>1) & 0x01);  //报警复位
		if(pRS485_New_Alarm != pRS485_Old_Alarm)
		{
		    gAlarmReset = pRS485_New_Alarm;
		    pRS485_Old_Alarm = pRS485_New_Alarm;
		}

		pRS485_New_Buzzer = ((rSlaver_State_Control1>>2) & 0x01);  //报警消音
		if(pRS485_New_Buzzer != pRS485_Old_Buzzer)
		{
		    gSlaver_ES_Buzzer = pRS485_New_Buzzer;
		    pRS485_Old_Buzzer = pRS485_New_Buzzer;

		    if(gSlaver_ES_Buzzer == 1)  //消音
		    {
		        gES_Buzzer = 0;  //消音
		    }
		    else  //未消音
		    {
		        gES_Buzzer = 1;
		    }
		}

		UP.Para.Type_FlowContr = ((rSlaver_State_Control1>>9) & 0x01);  //设备控流模式

		gState_Return = ((rSlaver_State_Control1>>12) & 0x01);  //气吹启停设置
	}
	else if(FP.Para.Com_Mode == 1)  //SMC_ASCII协议
	{
		pRS485_New_Run = rSlaver_State_Control1 & 0x0001;  //运行状态
	}
	else if(FP.Para.Com_Mode == 2)  //SMC_RTU协议
	{
		pRS485_New_Run = rSlaver_Operation & 0x01;  //运行状态
		pRS485_New_Mode = ((rSlaver_Operation>>1) & 0x01);  //通讯模式
		if(pRS485_New_Mode != pRS485_Old_Mode)
		{
		    UP.Para.Type_Communication = pRS485_New_Mode;
		    pRS485_Old_Mode = pRS485_New_Mode;
		}

		pRS485_New_Alarm = ((rSlaver_Operation>>2) & 0x01);  //报警复位
		if(pRS485_New_Alarm != pRS485_Old_Alarm)
		{
		    gAlarmReset = pRS485_New_Alarm;
		    pRS485_Old_Alarm = pRS485_New_Alarm;
		}

		//数据指令
		gSlaver_Data1 = (UP.Para.rSlaver_Data & 0x000F);  //数据指令1
		gSlaver_Data2 = ((UP.Para.rSlaver_Data>>4) & 0x000F);  //数据指令2
		gSlaver_Data3 = ((UP.Para.rSlaver_Data>>8) & 0x000F);  //数据指令3
		gSlaver_Data4 = ((UP.Para.rSlaver_Data>>12) & 0x000F);  //数据指令4
	}
	if(pRS485_New_Run != pRS485_Old_Run)
	{
		gState_Run = pRS485_New_Run;
		pRS485_Old_Run = pRS485_New_Run;
	}


}
static uint16_t DRI_MoubusRTU_getRegData(uint16_t add)//todo:根据不同的协议处理不同的数据
{
	/* WIIP通讯 */
	uint16_t pModbusSlaverUpData = 0;
	if(FP.Para.Com_Mode == 0)//WIIP协议
	{
		switch(add)
		{
		case 1:
			pModbusSlaverUpData = rSlaver_State_Control1;  //控制字1
			break;

		case 11:
			pModbusSlaverUpData = rSlaver_State_Chiller1;  //状态字1
			break;

		case 21:
			pModbusSlaverUpData = rSlaver_State_Alarm1;  //报警字1
			break;
		case 22:
			pModbusSlaverUpData = rSlaver_State_Alarm2;  //报警字2
			break;

		case 31:
			pModbusSlaverUpData = ((int16_t)(100*UP.Para.SV_Temp1));//主温度设定值
			break;
		case 32:
			pModbusSlaverUpData = 100*UP.Para.SV_Flow;  //流量设定值
			break;
		case 33:
			pModbusSlaverUpData = 10*UP.Para.SV_Press;  //压力设定值
			break;

		case 71:
			pModbusSlaverUpData = ((int16_t)(100*gPV_TempAbmi));  //环境温度实测值
			break;

		case 91:
			pModbusSlaverUpData = ((int16_t)(100*gPV_Temp1));//主温度实测值
			break;
		case 92:
			pModbusSlaverUpData = 10*gPV_Press_Out;//出液压力实测值
			break;
		case 93:
			pModbusSlaverUpData = ((int16_t)(100*gPV_Temp2));//辅温度实测值
			break;
		case 94:
			pModbusSlaverUpData = 10*gPV_Press_In;//回液压力实测值
			break;
		case 95:
			pModbusSlaverUpData = 100*gPV_Flow;//流量实测值
			break;
		case 96:
			pModbusSlaverUpData = 100*gPV_DI;//水质实测值
			break;
			default:
	//			gSlaver_State = 50;
			break;
		}
	}
	else if(FP.Para.Com_Mode == 1)  //SMC_ASCII协议
	{

	}
	else if(FP.Para.Com_Mode == 2)  //SMC_RTU协议
	{
		switch(add)
		{
			case 0x30:
				pModbusSlaverUpData=((int16_t)(10*gPV_Temp1));//主温度实测值
				break;
			case 0x34:
				pModbusSlaverUpData=((int16_t)(gPV_Press_Out));//出液压力实测值
				break;
			case 0x36:
				pModbusSlaverUpData=((int16_t)(10*gPV_Flow));//流量实测值
				break;
			case 0x38:
				pModbusSlaverUpData=((int16_t)(10*gSlaver_DisplayData1));//数据显示1
				break;
			case 0x39:
				pModbusSlaverUpData=((int16_t)(10*gSlaver_DisplayData2));//数据显示2
				break;
			case 0x3A:
				pModbusSlaverUpData=((int16_t)(10*gSlaver_DisplayData3));//数据显示3
				break;
			case 0x3B:
				pModbusSlaverUpData=((int16_t)(10*gSlaver_DisplayData4));//数据显示4
				break;
			case 0x3C:
				pModbusSlaverUpData=rSlaver_State_Chiller;//设备状态组
				break;
			case 0x3D:
				pModbusSlaverUpData=rSlaver_State_Alarm1;//报警状态组1
				break;
			case 0x3E:
				pModbusSlaverUpData=rSlaver_State_Alarm2;//报警状态组2
				break;
			case 0x3F:
				pModbusSlaverUpData=rSlaver_State_Alarm3;//报警状态组3
				break;
			case 0x40:
				pModbusSlaverUpData=((int16_t)(10*UP.Para.SV_Temp1));//主温度设定值
				break;
			case 0x42:
				pModbusSlaverUpData=rSlaver_Operation;//操作指令
				break;
			case 0x43:
				pModbusSlaverUpData=UP.Para.rSlaver_Data;//数据指令
				break;
			default:
	//			gSlaver_State = 50;
			break;
		}
	}

	return pModbusSlaverUpData;
}

static void DRI_MoubusRTU_setRegData(uint16_t add)
{
	//todo:根据不同的协议处理不同的数据
	if(FP.Para.Com_Mode == 0)//WIIP协议
	{
		/* WIIP通讯 */
		if(add >= 0)
		{
			switch(add)
			{
			case 1:
				rSlaver_State_Control1 = (gBus485_Slaver_RxBuffer[4]<<8) + gBus485_Slaver_RxBuffer[5];//控制字1
				break;

			case 31:
				UP.Para.SV_Temp1 = ((float)(int16_t)((gBus485_Slaver_RxBuffer[4]<<8) + gBus485_Slaver_RxBuffer[5]))/100;//主温度设定
				break;
			case 32:
				UP.Para.SV_Flow = ((gBus485_Slaver_RxBuffer[4]<<8) + gBus485_Slaver_RxBuffer[5])/100;//流量设定值
				break;
			case 33:
				UP.Para.SV_Press = ((gBus485_Slaver_RxBuffer[4]<<8) + gBus485_Slaver_RxBuffer[5])/10;//压力设定值
				break;
			default:
//				gSlaver_State = 50;
				break;
			}
		}
	}
	else if(FP.Para.Com_Mode == 1)  //SMC_ASCII协议
	{

	}
	else if(FP.Para.Com_Mode == 2)  //SMC_RTU协议
	{
		if(add >= 0)
		{
			switch(add)
			{
			case 0x40:
				UP.Para.SV_Temp1 = ((float)(int16_t)((gBus485_Slaver_RxBuffer[4]<<8) + gBus485_Slaver_RxBuffer[5]))/10;//主温度设定
				break;
			case 0x42:
				rSlaver_Operation = (gBus485_Slaver_RxBuffer[4]<<8) + gBus485_Slaver_RxBuffer[5];//操作指令
				break;
			case 0x43:
				UP.Para.rSlaver_Data = (gBus485_Slaver_RxBuffer[4]<<8) + gBus485_Slaver_RxBuffer[5];//数据指令
				break;
			default:
//				gSlaver_State = 50;
				break;
			}
		}
	}
}

static void DRI_MoubusRTU_setRegDatas(uint16_t add, uint8_t cnt)
{
	uint16_t data = 0;
	data = (uint16_t)((gBus485_Slaver_RxBuffer[7 + cnt] << 8) + gBus485_Slaver_RxBuffer[8 + cnt]);

	if(FP.Para.Com_Mode == 0)//WIIP协议
	{
		switch(add)
		{
		case 1:  //控制字1
			rSlaver_State_Control1 = data;
			break;
		case 31:  //主温度设定值
			UP.Para.SV_Temp1 = ((float)(int16_t)(data))/100;
			break;
		case 32:  //流量设定值
			UP.Para.SV_Flow = ((float)data/100);
			break;
		case 33:  //压力设定值
			UP.Para.SV_Press = ((float)data/10);
			break;
		default:
		//gSlaver_State = 50;
			break;
		}
	}
	else if(FP.Para.Com_Mode == 1)  //SMC_ASCII协议
	{

	}
	else if(FP.Para.Com_Mode == 2)  //SMC_RTU协议
	{
		 switch(add)
		 {
			case 0x40:  //主温度设定值
				UP.Para.SV_Temp1 = ((float)(int16_t)data)/10;
				break;
			case 0x42:  //操作指令
				rSlaver_Operation = data;
				break;
			default:
	//				gSlaver_State = 50;
				break;
		}
	}


}
